package com.zoweunion.mechanic.util.task;

import com.zoweunion.mechanic.dao.base.OwnerDao;
import com.zoweunion.mechanic.service.base.LockCarService;
import com.zoweunion.mechanic.service.base.OwnerService;
import com.zoweunion.mechanic.util.Orgin;
import com.zoweunion.mechanic.util.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *金融平台-服务商逾期锁车定时任务
 *注：当天不锁车，延后一天凌晨三点锁车
 */
@Component
public class FinancialPlatformLockCarTask {

    @Autowired
    private OwnerService ownerService;
    @Autowired
    private LockCarService lockCarService;
    @Autowired
    private OwnerDao ownerDao;
    @Autowired
    private CarDataAbnormalTask carDataAbnormalTask;

    /**
     * 每日凌晨三点执行一次
     */
    // @Scheduled(cron = "*/60 * * * * ? ")
    @Scheduled(cron = "0 0 3 * * ? ") // 每日凌晨三点执行一次
    private void lockCarTask(){
        //1.查询 所有状态值不等于1 且 当前时间超过账期时间大于一天 的账期 即：未还款或者延期还款、当前时间超过还款时间等于一天的账期
        List<Map<String ,Object>> noRepaymentStatementList = ownerService.selectAllNoRepaymentStatement();
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("lock_user_id", "系统平台");
        paramMap.put("create_user", "系统平台");
        paramMap.put("update_user", "系统平台");
        paramMap.put("user_name", "系统平台");
        paramMap.put("isLocked", 1);
        paramMap.put("lock_type", 2);
        paramMap.put("smsStatus", 1);
        // lock_speed
        // lock_torque
        //分两种情款，一种是延期的、一种是逾期的
        if(noRepaymentStatementList.size() > 0){
            noRepaymentStatementList.forEach(item ->{
                //2.如果status == 0 and parent_id == null 则直接调用锁车(逾期)
                if(Integer.valueOf(item.get("status").toString()) == 0 && item.get("parent_id") == null){
                    paramMap.put("car_id", item.get("car_id").toString());
                    paramMap.put("s_id", item.get("s_id").toString());
                    paramMap.put("things_id", item.get("things_id").toString());
                    lockCarService.thingsIdCarOperation(paramMap);
                }else if(item.get("parent_id") != null){
                    //延期(可能延期多次,需要查询最近的一次延期，最近一次如果就是本次则锁车)
                    Map<String, Object> map = new HashMap<>();
                    map.put("payment_id", item.get("parent_id").toString());
                    map.put("parent_id", item.get("parent_id").toString());
                    map.put("car_id", item.get("car_id").toString());
                    //查询最近一次的延期时间
                    Map<String, Object> delayRepayLatestInfo = ownerDao.selectDelayRepayLatestInfo(map);
                    //最近一次的id 与当前id 相等则锁车
                    if(delayRepayLatestInfo.get("id").toString().equals(item.get("id").toString())) {
                        Map<String, Object> repayInfo = ownerDao.selectRepaymentStatementInfo(map);
                        if (Integer.valueOf(repayInfo.get("status").toString()) == 2) {
                            paramMap.put("car_id", item.get("car_id").toString());
                            paramMap.put("s_id", item.get("s_id").toString());
                            paramMap.put("things_id", item.get("things_id").toString());
                            lockCarService.thingsIdCarOperation(paramMap);
                        }
                    }
                }
            });
        }
    }

    /**
     * 每日凌晨四点执行一次
     * 临近还款日推送、短信提醒(还款日前七天、三天、一天)
     */
    // @Scheduled(cron = "*/60 * * * * ? ")
    @Scheduled(cron = "0 0 4 * * ? ") // 每日凌晨四点执行一次
    public void remindTask() {
        Map<String, Object> paramMap = new HashMap<>();
        //定义数组方便后期扩展，可根据用户动态设置提前几天提醒
        List<Integer> dateList = new ArrayList<>();
        dateList.add(7);
        dateList.add(3);
        dateList.add(1);
        dateList.forEach(day->{
            //1.查询 所有状态值不等于1 且 当前时间距离还款日#{day}天的的账期
            List<Map<String ,Object>> noRepaymentStatementByDayList = ownerService.selectAllNoRepaymentStatementByDay(day);
            if(noRepaymentStatementByDayList.size() > 0){
                noRepaymentStatementByDayList.forEach(item ->{
                    //2.如果status == 0 and parent_id == null 则直接提醒还有#{day}天还款
                    if(Integer.valueOf(item.get("status").toString()) == 0 && item.get("parent_id") == null){
                        List<String> thingsList = new ArrayList<>();
                        thingsList.add(item.get("things_id").toString());
                        //根据things_id查询车主、司机 carDataAbnormalTask.getUserList(thingsList,0)第二个参数没有用到，根据之前的定义暂未修改，暂时传0(无意义）
                        List<Map<String, Object>> userList = carDataAbnormalTask.getUserList(thingsList,0);
                        userList.forEach(user->{
                            if(user.get("o_id") != null && user.get("o_id").toString() != "") {
                                carDataAbnormalTask.jPushRemindRepaymentByDay(user, Orgin.getString(user, "o_id"), day);
                            }
                            if(user.get("d_id") != null && user.get("d_id").toString() != "") {
                                carDataAbnormalTask.jPushRemindRepaymentByDay(user, Orgin.getString(user, "d_id"), day);
                            }
                            String adviceName = Orgin.getString(user, "advice_name") != "" ? Orgin.getString(user, "advice_name") : "无";
                            String model = Orgin.getString(user, "model") != "" ? Orgin.getString(user, "model") : "无";
                            String brand = Orgin.getString(user, "brand")!= "" ? Orgin.getString(user, "brand") : "无";
                            String car_type = Orgin.getString(user, "car_type")!= "" ? Orgin.getString(user, "car_type") : "无";
                            String content = String.format(SmsUtil.WarningRemindRepaymentByDay, adviceName, model, brand, car_type, day);
                            if(user.get("o_mobile_phone") != null && user.get("o_mobile_phone").toString() != "") {
                                SmsUtil.sendSms(Orgin.getString(user, "o_mobile_phone"), content);
                            }
                            if(user.get("d_mobile_phone") != null && user.get("d_mobile_phone").toString() != "") {
                                SmsUtil.sendSms(Orgin.getString(user, "d_mobile_phone"), content);
                            }
                        });
                    }else if(item.get("parent_id") != null){
                        //延期(可能延期多次,需要查询最近的一次延期，最近一次如果就是本次则提醒还有#{day}天还款)
                        Map<String, Object> map = new HashMap<>();
                        map.put("payment_id", item.get("parent_id").toString());
                        map.put("parent_id", item.get("parent_id").toString());
                        map.put("car_id", item.get("car_id").toString());
                        //查询最近一次的延期时间
                        Map<String, Object> delayRepayLatestInfo = ownerDao.selectDelayRepayLatestInfo(map);
                        //最近一次的id 与当前id 相等则提醒还有#{day}天还款
                        if(delayRepayLatestInfo.get("id").toString().equals(item.get("id").toString())) {
                            Map<String, Object> repayInfo = ownerDao.selectRepaymentStatementInfo(map);
                            if (Integer.valueOf(repayInfo.get("status").toString()) == 2) {
                                List<String> thingsList = new ArrayList<>();
                                thingsList.add(item.get("things_id").toString());
                                //根据things_id查询车主、司机 carDataAbnormalTask.getUserList(thingsList,0)第二个参数没有用到，根据之前的定义暂未修改，暂时传0(无意义）
                                List<Map<String, Object>> userList = carDataAbnormalTask.getUserList(thingsList,0);
                                userList.forEach(user->{
                                    if(user.get("o_id") != null && user.get("o_id").toString() != "") {
                                        carDataAbnormalTask.jPushRemindRepaymentByDay(user, Orgin.getString(user, "o_id"), day);
                                    }
                                    if(user.get("d_id") != null && user.get("d_id").toString() != "") {
                                        carDataAbnormalTask.jPushRemindRepaymentByDay(user, Orgin.getString(user, "d_id"), day);
                                    }
                                    String adviceName = Orgin.getString(user, "advice_name") != "" ? Orgin.getString(user, "advice_name") : "无";
                                    String model = Orgin.getString(user, "model") != "" ? Orgin.getString(user, "model") : "无";
                                    String brand = Orgin.getString(user, "brand")!= "" ? Orgin.getString(user, "brand") : "无";
                                    String car_type = Orgin.getString(user, "car_type")!= "" ? Orgin.getString(user, "car_type") : "无";
                                    String content = String.format(SmsUtil.WarningRemindRepaymentByDay, adviceName, model, brand, car_type, day);
                                    if(user.get("o_mobile_phone") != null && user.get("o_mobile_phone").toString() != "") {
                                        SmsUtil.sendSms(Orgin.getString(user, "o_mobile_phone"), content);
                                    }
                                    if(user.get("d_mobile_phone") != null && user.get("d_mobile_phone").toString() != "") {
                                        SmsUtil.sendSms(Orgin.getString(user, "d_mobile_phone"), content);
                                    }
                                });
                            }
                        }
                    }
                });
            }
        });
    }
}
